package guanqu.server;

import java.io.IOException;
import java.util.Calendar;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.junit.Test;

import com.alibaba.fastjson.JSON;

import guanqu.beans.SluiceControl;
import manager.PersistentManager;

/**
 * 闸门控制 Servlet implementation class GetSluiceCOntrol
 */

public class DoSluiceControl extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public DoSluiceControl() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		// response.getWriter().append("Served at:
		// ").append(request.getContextPath());

		SessionFactory sf = PersistentManager.sharemanager().getSessionFactory();
		// Session session = sf.openSession();
		Session session = sf.getCurrentSession();

		Transaction tx = session.beginTransaction();
		try {

			String sData = request.getParameter("data");
			UploadSluiceControlData uploadSluiceControlData = JSON.parseObject(sData, UploadSluiceControlData.class);
			if (uploadSluiceControlData == null) {
				System.out.println("null");
				return;
			}
			session.save(uploadSluiceControlData.getSc());

//			// 根据闸门编号，查找闸门接收人员
//			IrrBAWaterGate gate = (IrrBAWaterGate) session.get(IrrBAWaterGate.class,
//					uploadSluiceControlData.getSc().getWatergateCode());
//
//			// 根据用户姓名查出用户帐号
//			Query qUser = session.createQuery("from User u where u.realName=?");
//			qUser.setParameter(0, gate.getEngManCd());
//			if (!qUser.list().isEmpty()) {
//				User u = (User) qUser.list().get(0);
//
//				// 根据帐号通知在线用户
//				Notify.ControlGate controlGate = new Notify.ControlGate();
//				controlGate.setSc(uploadSluiceControlData.getSc());
//				InternalMananger.share().notify(u.getAccount(), JSON.toJSONString(controlGate));
//			}
			
			// TODO 测试通知所有在线用户
		/*	Notify.ControlGate controlGate = new Notify.ControlGate();
			controlGate.setSc(uploadSluiceControlData.getSc());
			notifyAllControl(JSON.toJSONString(controlGate));*/
			

			tx.commit();
		} catch (Exception e) {
			tx.rollback();
			e.printStackTrace();
		} finally {
			// session.close();
			response.getWriter().append("success");
		}

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}
	
	
	// 通知所有用户
/*	private void notifyAllControl(String msg)
	{
		InternalMananger.share().notifyAllChannel(msg);
	}*/

	// 读取客户端发送过来的数据
	public static class UploadSluiceControlData {
		SluiceControl sc = new SluiceControl();

		public SluiceControl getSc() {
			return sc;
		}

		public void setSc(SluiceControl sc) {
			this.sc = sc;
		}
	}

	@Test
	public void test1() {
		UploadSluiceControlData uploadSluiceControlData = new UploadSluiceControlData();
		SluiceControl sControl = new SluiceControl();
		uploadSluiceControlData.setSc(sControl);
		sControl.setStadiaCode("10510001");
		sControl.setSendPerson("leikai");
		sControl.setSendTime(Calendar.getInstance());
		System.out.println("JSON data = " + JSON.toJSONString(uploadSluiceControlData));
	}
}
